package com.hxd.qzg.module._admin.login;


import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.hxd.qzg.common.BaseController;
import com.hxd.qzg.common.Result;
import com.hxd.qzg.common.ServiceException;
import com.hxd.qzg.common.model.Box;
import com.jfinal.plugin.activerecord.Page;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

public class BoxController extends BaseController {

    private static final Log logger = LogFactory.get();

    /**
	 * 盒子信息
	 */

	public void list() {


		String start = get("start");
		String end = get("end");
		String key = get("key");
		String fromSql = "FROM api_box WHERE api_box.is_delete=0 ";

		if (StringUtils.isNotEmpty(key)) {
			fromSql = fromSql + " AND ( name = '" + key + "' OR box_group_id = '" + key + "')";
		}

		if (StringUtils.isNotEmpty(start)) {
			fromSql = fromSql + " AND last_date >= '" + start + "'";
		}
		if (StringUtils.isNotEmpty(end)) {
			fromSql = fromSql + " AND last_date <= '" + end + "'";
		}

		Page<Box> page = Box.dao.paginate(getInt("pageNum"), getInt("pageSize"), "select *",
				fromSql);

		renderJson(Result.success(page));
    }

	 /**
     * 盒子标记异常与恢复
     */

		public void ex() {
			String id = get("id");
			if (StringUtils.isEmpty(id)) {
				throw new ServiceException("id 不能为空");
			}
			String status = get("status");
			if (StringUtils.isEmpty(status)) {
				throw new ServiceException("status 不能为空");
			}
			Box box = new Box();
			box.setId(Long.valueOf(id));
			box.setStatus(Integer.valueOf(status));
			box.setUpdateTime(new Date());
			box.setLastDate(new Date());
			box.update();
			renderJson(Result.success());
    }
}
